home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Commodore Free 8
/
Commodore_Free_Issue_08_2007_Commodore_Computer_Club.d64
/
t.ls prg part ii
< prev
next >
Wrap
Text File
|
2023-02-26
|
12KB
|
491 lines
uMr. LOADSTAR's Intro to Programming
the C64 Part II By Dave Moorman
Now you have certainly played around
with the PRINT statement. The question
arises -- how can I put the text
exactly where I want it on the screen?
We have several ways. You might have
figured out the first. Use embedded
cursor keystrokes. This is easy
10 ?[clr][down][down][right][right]
[right]Test
And it looks like a mess in your
program, with all those reversed
characters. There are two better ways.
The first is a routine available from
the Kernal ROM. These routines are what
BASIC uses to do its magic. But we can
use them directly with a couple of new
(to you) commands: POKE & SYS. NEW your
memory & try this:
5 ?"[clr]"
10 x=782:y=781:set=783:plot=65520
20 poke x,10:poke y,10:poke set,0
30 sys plot:?"This can be centered"
PLOT is a Machine Language routine. We
use POKE (you can use a shortcut of
"pO" to save typing) to put the X
column in memory location 782 & the Y
row in location 781. SET (783) must be
set to 0 for this to work. Then the SYS
command executes ML code at 65520. You
will notice that we can put more than
one command on a program line,
separated by colons. Actually, you have
two screen lines available for each
program line. Back to plotting text on
the screen. The third way is to use a
poke & the TAB command.
50 row=15:col=10
60 poke 214,row-1
70 ?tab(col)"This can be centered"
The key here is the PRINT after poking
ROW-1 in location 214. If you want to
put your text on the top line, use
PRINT"[home]".
Either way of plotting is just fine --
you choose.
LOOPING THE LOOP
While all this is fun, the greatest
power of a computer is to do things
over & over again. Repetition makes the
world go round! NEW your memory & type
in this simple code.
10 ?"[clr]"
20 ?"Hello, World"
30 goto 20
Before you run this, try to figure out
what will happen. The GOTO command is
new, but it is fairly obvious. Now, run
the program.
WHOA! Look at it go! When will it stop?
It won't. This is called an "infinite
loop," since it will infinitely print
"Hello, World!" on line 20, then goto
line 20, where it prints but you get
the idea. In fact, you may be looking
at it in action, wondering what to do.
Since you are programming in BASIC, the
answer is simple. Press [STOP]. The
result is not particularly elegant,
what with
break in 20
ready.
appearing on the screen. But this IS an
infinite loop. Be happy we have a
[STOP] key! (Have you ever had your
Windows PC "hang?" No combination of
keys, not even the [CTRL-ALT-DELETE]
will get back control. Guess what kind
of error some programmer made! Yep --
an infinite loop -- with no way for the
user to get out. That can happen in ML.
Infinity is great, but we need more
control. More POWER! We need to be able
to stop the loop when we want it to
stop -- when the conditions are right.
Behold! The "conditional loop!"
10 ?"[clr]"
15 x=0
20 ?"Hello, World!"
30 x=x+1
40 if x<10 then 20
50 end
Here, we use X as a counter. You do not
have to zero out X as in line 15, but
it is a good idea. You never know where
that X has been! Line 20 prints the
text, as before. Then line 30
increments X. Now, if you are not
familiar with programming, saying X=X+1
sounds a little crazy, since in
algebra, X can never equal X+1. But the
equal sign here is not equal. It is the
sign to assign X with the value of X+1.
Long ago, even before home computers,
BASIC had a command for this. Change
line 30 to
30 let x=x+1
Now, as you read this out loud, the
equal sign makes more sense, since LET
lets X change its value. Think of it
this way. X is a box, right? Inside
that box, at the beginning of the
program is a value of nothing. So, we
take the nothing out of the box, add
one to it, & put the result back in the
box. The program loops, & now X
contains 1. We take out the 1, add 1, &
put the result back into the box. This
is exactly what happens with any value
assignment. You don't need the LET
command, so don't waste your time or
your computer's memory. Whenever a
variable is followed by an equal sign,
the genie knows an assignment is about
to happen. Now that you understand
incrementing, lets look at line 40.
This is the IF-THEN command. The genie
looks at the comparison following the
IF. In this case it is X<10. That's "X
is less than 10." If that is true, the
THEN happens -- in this case, the
program goes to line 20. If X is not
less than 10, the IF-THEN is said to
"fall through," & the next program line
is executed.
Any two numeric values (variables or
constants) or any two strings
(variables or literals) can be compared
this way, using one of these
comparisons:
Equals =
Greater Than >
Less Than <
Greater or Equal => or >=
Less or Equal <= or =<
Not Equal <>
The IF-THEN command is what gives the
computer its intelligence. You might
have noticed that the genie is not too
bright. It does exactly what you tell
it to -- if it understands what you
mean. (I will bet you have suffered a
lot of SYNTAX ERRORs!) So with the
IF-THEN command, we tell the genie to
change the flow of the program when a
certain condition applies. And counting
is just one possible condition. Here is
another
40 if peek(198)=0 then 20
45 poke198,0
Here we are using another "system
resource," a location in memory that
BASIC uses for its own purposes.
Location 198 holds how many times a key
has been pressed since the last time
keypresses were collected by the
system. And, how about that -- we have
encountered another command. PEEK(loc)
peeks under BASIC right into a memory
location itself. And we are fortunate
to have PEEK, POKE, & SYS, because the
C64 has a lot more power than BASIC 2.0
can handle.
In this case, we are looking at the
keyboard queue, the number of
keystrokes waiting to be processed. If
0, then the program loops. If not, we
POKE a 0 into 198 (to clean things up a
bit), & end the program. I probably
should have put 198 in a variable
16 key=198
40 if peek(key)=0 then 20
45 poke key,0
Looks much nicer, eh? However, as you
learn your way around the C64, you will
discover 198 is one of those locations
you will naturally learn by heart.
As with all programming, we have more
than one way to do most anything. That
is part of the fun -- finding the best
way -- for speed and/or elegance -- to
accomplish a goal. Time for another
tiny program, so NEW & enter:
10 ?"[clr]"
20 ?"Hello, World!"
30 getz$
40 if z$="" then 20
The GET command gets a keystroke, if
any, & puts it in the string variable
you designate. I always use Z$. It is a
habit of mine. In fact, I use Z$ for
nothing else.The comparison in line 40
is to see if Z$ holds anything -- or
rather, if it is equal to nothing,
which is indicated by 2 double-quotes.
As long as it holds nothing, the
program loops. This is more elegant,
than the PEEK(198) -- & full of
possibilities.
Because we can string strings together.
Did I mention "concatenation?" It is a
powerful ability of BASIC. Here is yet
another small program to try. I do hope
you are typing these in, looking them
over, running them, then listing &
looking at them again. I know I am,
even as I write.
10 ?"[clr]"
20 w$="":c$="<"
30 ?"[home]"w$c$
40 getz$:if z$="" then30
50 if z$=chr$(13) then end
60 w$=w$+z$
70 goto30
You just wrote your first attempt at a
word processor! It's not a Good word
processor. In fact, as you play with
it, you might notice it's not even a
good input routine. You will notice
this most if you try using [Delete] or
a cursor keystroke. The displayed text
gets all messy.
But lets look at what we have done. We
put nothing ("") in W$, & a little
pointer ("<") in c$. Then we print them
on the home row in line 30. Notice, you
don't have to use semi-colons between
string variables. The $ tells the
computer where each variable ends.
Then we get Z$. If it is empty (called
"null"), we loop back to the same line
number. OOPS! We loop to the print
line. Change line 40 to go to line 40.
It is neater that way. If a key has
been pressed, line 60 adds
(concatenates) Z$ to the end of W$.
(What would happen if you used
W$=Z$+W$? Then the program loops.
Now line 50 is interesting. We must
check Z$ for a [RETURN] key press. But
we cannot use a [RETURN] in the program
line, because it will enter the line
into memory. So we must use a CHR$(n),
which turns a number into a character
string. You might try this:
?chr$(65)
You should see an "a" printed on the
screen. Every character has a number.
In fact, inside the computer, there are
no characters -- only numbers. CHR$(13)
is the RETURN character. This is
another number you will memorize.
So, if Z$ holds a RETURN, then the
program ends. That simple.
But how do we get rid of those things
that mess up the printing of our line?
If you guessed, "Using IF-THEN
commands," you are right. Now to figure
out what numbers to put in such
commands.
Add these two lines to the top of your
program:
1 getz$:ifz$="" then1
2 ?asc(z$):goto1
ASC($) returns the number you put in
CHR$(n) to get the character. So press
[a]. Yep -- 65. Lets try several other
characters we do not want in W$.
Delete 20
Insert 148
Cursor Up 145
Cursor Dn 17
Cursor Lft 157
Cursor Rt 29
Home 19
CLR 147
COLORS:
Black 144
White 5
Red 28
Cyan 159
Purple 156
Green 30
Blue 31
Yellow 158
Orange 129
Brown 149
Lt Red 150
Dk Gray 151
Med Gray 152
Lt Green 153
Lt Blue 154
Lt Gray 155
Now, that is quite a list -- & doing an
IF-THEN for each one would take a lot
of lines -- & a lot of time. But
perhaps you see a pattern here? These
numbers fall into two ranges: 5 - 31 &
144 - 159. So we can eliminate these
keystrokes with 2 IF-THEN
commands. First, break out of this loop
by pressing [STOP]. Then type
1 [RETURN]
2 [RETURN]
That is all you need to do to remove a
program line -- enter its line number.
Now add these two lines:
54 z=asc(z$)
55 if z>=5 & z<=31 then 40
56 if z>=144 & z<=159 then 40
List your program & follow the logic.
We have used ASC(z$) to get the
character value (called the ASCII
value) of the key press. Then we see if
Z is Greater Than or Equal To 5 AND Z
is Less Than or Equal To 31. The AND
means that both conditions must be true
for the IF to be true. Imagine Z= 29.
Is 29 Greater or Equal to 5? YES.Is 29
Less or Equal to 31? YES. Then 29 is in
the range of 5 - 31 -- & we loop back
up to line 40. The same works in line
56 for the higher range.
AND is a Logic Operator, & works like:
A AND B Result
False False False
False True False
True False False
True Ture True
So, only when A AND B are True, then
the Result is True.We have another
Logic Operator we can use in IF-THEN
commands: OR
A OR B Result
False False False
False True True
True False True
True True True
If either A OR B (or both) are True,
then the Result is True.
Now with this, we can put both IF-THENs
into one:
55 IF (z>=5 AND z<=31) or (z>=144 AND
Z<=59) THEN 40
As with math, the comparisons are done
in the parentheses first. So if Z is in
either range, then the program loops.
Now -- that may have you scratching
your head. Don't worry. Many concepts
take time to take root. So, while we
are on the subject, let's look at
exactly happens in a comparison.
1 a = 7
2 ?a<10
3 ?a>10
4 end
Run this dab of code. You should get
-1
0
In BASIC on the C64, true is -1 & false
if 0. And if you must know, the IF-THEN
command only worries about false. Add
these lines:
4 if 0 then ?"False"
5 if 1 then ?"True"
6 stop
You will see that checking for a 0 in a
variable is very easy. These two lines
do exactly the same thing:
if a<>0 then 50000
if a then 50000
But if I confused you, I do apologize.
Or better yet, PLAY with it. We have
covered a lot of ground in this
section. We have given you almost
enough to write an arcade game!
You know how to print.
You know how to position your printing
on the screen. (sys plot)
You know how to get keystrokes. (getz$)
You know how to do math with numeric
variables.
You know how to use IF-THEN commands to
add intelligence to your computer.
What is left?
Lots of stuff! But you have the
essentials. Try to use the cursor keys
to move a dot around the screen. Here
is a good piece of code:
150 getz$:ifz$=""then 150
160 ifz$="[left]" then x=x-1
161 ifz$="[right]" then x=x+1
162 ifz$="[up]" then y=y-1
163 ifz$="[down]" then y=y+1
That should get you started
The more you fiddle around with these
ideas, the more you will be ready for
our next lesson.